package com.jinyu.jyly.ui.mine.transfer;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.MobileToNickNameInfo;
import com.jinyu.jyly.utils.AES;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 账户互转
 */
public class TransferAccountActivity extends BaseActivity<TransferAccountPresenter> implements TransferAccountContract.View {

    @BindView(R.id.fake_status_bar)
    View fakeStatusBar;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_title_view)
    RelativeLayout rlTitleView;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.tv_tran_msg)
    TextView tvTranMsg;
    @BindView(R.id.et_tran_num)
    EditText etTranNum;
    @BindView(R.id.tv_allow_tran_num)
    TextView tvAllowTranNum;
    @BindView(R.id.btn_sub)
    Button btnSub;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.tv_title)
    TextView tv_title;
 @BindView(R.id.tv_danwei)
    TextView tv_danwei;

    private TransferAccountPresenter presenter;
    private int type;

    @Override
    public int getLayout() {
        return R.layout.activity_transfer_account;
    }

    @Override
    protected TransferAccountPresenter getPresenter() {
        if (presenter == null) {
            presenter = new TransferAccountPresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
        initIntent();
    }

    private void initIntent() {
        Intent intent = getIntent();
        type = intent.getIntExtra("type", 1);
        switch (type) {
            case 1:
                //宝石
                float pdb = intent.getFloatExtra("pdb", 0.0f);
                tvAllowTranNum.setText(pdb + "宝石");
                tv_title.setText("账户互转宝石");
                tvTranMsg.setText("最少转出数量限制为100宝石");
                tv_danwei.setText("宝石");
                break;
            case 2:
                //元宝
                float pdbs = intent.getFloatExtra("pdb", 0.0f);
                tvAllowTranNum.setText(pdbs + "钻石");
                tv_title.setText("账户互转钻石");
                tvTranMsg.setText("最少转出数量限制为100钻石");
                tv_danwei.setText("钻石");
                break;
            default:
                break;
        }

    }

    @Override
    public void onFailer(Throwable throwable) {

    }

    /**
     * 提交转账
     *
     * @param info
     */
    @Override
    public void onTransfer(AllInfoBean info) {
        mTipDialog.dismiss();
        switch (info.getStatus()) {
            case 200:
                String beanData = info.getData();
                String result = null;
                try {
                    result = AES.decrypt(beanData);
                    Gson gson = new Gson();
                    showToast("转账成功");
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(info.getMsg());
                break;
            default:
                break;
        }
    }

    /**
     * 根据手机号获取昵称
     *
     * @param infoBean
     */
    @Override
    public void onGetNickName(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        switch (infoBean.getStatus()) {
            case 200:
                String beanData = infoBean.getData();
                String result = null;
                try {
                    result = AES.decrypt(beanData);
                    Gson gson = new Gson();
                    MobileToNickNameInfo mobileToNickNameInfo = gson.fromJson(result, MobileToNickNameInfo.class);
                    if (mobileToNickNameInfo != null) {
                        tv_nickname.setText(mobileToNickNameInfo.getRealname());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_get_code, R.id.btn_sub})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_get_code:
                //验证姓名
                String mobiles = etMobile.getText().toString().trim();
                if (TextUtils.isEmpty(mobiles)) {
                    showToast("手机号码不能为空");
                    return;
                }
                try {
                    mTipDialog.show();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("mobile", mobiles);
                    String pamam = AES.encryptToHexString(jsonObject.toString());
                    presenter.getUserName(pamam);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.btn_sub:
                //提交
                String mobile = etMobile.getText().toString();
                String tranNum = etTranNum.getText().toString();
                if (TextUtils.isEmpty(mobile)) {
                    showToast("手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(tranNum)) {
                    showToast("转出数量不能为空");
                    return;
                }
                transferGiftDialog();
                break;
        }
    }

    /**
     * 转账礼包
     */
    private Dialog mtransferDialog;

    private void transferGiftDialog() {
        mtransferDialog = new Dialog(this, R.style.wheelviewDialog);
        mtransferDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        View outerView = LayoutInflater.from(this).inflate(R.layout.dialog_pay_psw, null);
        ImageView iv_close_open = outerView.findViewById(R.id.iv_close_open);
        TextView tv_close = outerView.findViewById(R.id.tv_close);
        TextView tv_sure = outerView.findViewById(R.id.tv_sure);
        TextView payPsw = outerView.findViewById(R.id.et_pay_psw);
        mtransferDialog.setCanceledOnTouchOutside(false);
        mtransferDialog.setContentView(outerView);
        // 将对话框的大小按屏幕大小的百分比设置
        WindowManager windowManager = this.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        Window window = mtransferDialog.getWindow();
        WindowManager.LayoutParams lp = mtransferDialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth() * 0.72); //设置宽度
//      lp.height = (int) (display.getHeight() * 0.68);//设置高度
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        mtransferDialog.show();
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String payPsws = payPsw.getText().toString();
                String mobile = etMobile.getText().toString();
                String tranNum = etTranNum.getText().toString();
                if (TextUtils.isEmpty(payPsws)) {
                    showToast("支付密码不能为空");
                    return;
                }

                boolean showing = mTipDialog.isShowing();
                if (!showing) {
                    switch (type){
                        case 1:
                            //宝石
                            try {
                                mTipDialog.show();
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("mobile", mobile);
                                jsonObject.put("num", Float.valueOf(tranNum));
                                jsonObject.put("password", payPsws);
                                String pamam = AES.encryptToHexString(jsonObject.toString());
                                presenter.setTransfer(pamam);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            break;
                        case 2:
                            //转账
                            try {
                                mTipDialog.show();
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("mobile", mobile);
                                jsonObject.put("num", Float.valueOf(tranNum));
                                jsonObject.put("password", payPsws);
                                jsonObject.put("bi","ingot");
                                String pamam = AES.encryptToHexString(jsonObject.toString());
                                presenter.setTransfer(pamam);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            break;
                        default:break;
                    }
                    mtransferDialog.dismiss();
                }
            }
        });

        iv_close_open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mtransferDialog.dismiss();
            }
        });

        tv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mtransferDialog.dismiss();
            }
        });
    }


}
